﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using CUWo.App.Resources;
using CUWo.Common;
using CUWo.Common.Entities;
using CUWo.Common.StorageHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;

namespace CUWo.App.WoControls
{
    public partial class WoUserCenterConfig : UserControl
    {
        public WoUserCenterConfig()
        {
            InitializeComponent();
        }

        #region 推送
        private void tgsPush_Checked(object sender, RoutedEventArgs e)
        {
            TextBlock content = FindVisualChild(this.tgsPush, "txtToggleSwitchStatus");
            if (content != null)
            {
                //this.tgsPush.IsEnabled = false;
                content.Text = "开启";
                this.UpdatePushStatusToAppRom(true);
                //this.tgsPush.IsEnabled = true;
            }
        }

        private void tgsPush_Unchecked(object sender, RoutedEventArgs e)
        {
            TextBlock content = FindVisualChild(this.tgsPush, "txtToggleSwitchStatus");
            if (content != null)
            {
                //this.tgsPush.IsEnabled = false;
                content.Text = "关闭";
                this.UpdatePushStatusToAppRom(false);
                //this.tgsPush.IsEnabled = true;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pushStatus">true:表示启用推送，用户接收消息</param>
        void UpdatePushStatusToAppRom(bool pushStatus = true)
        {
            if (IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                var romInfoString = IsolatedStorageHelper.Instance.ReadText(Keys.ROM_FILENAME);
                if (!string.IsNullOrWhiteSpace(romInfoString))
                {
                    RomInfo romInfo = JSONHelper.Deserialize<RomInfo>(romInfoString);
                    if (romInfo == null)
                    {
                        romInfo = new RomInfo();
                        romInfo.Mobile = string.Empty;
                        romInfo.Password = string.Empty;
                    }
                    romInfo.IsPush = pushStatus;
                    IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
                }
                else
                {
                    RomInfo romInfo = new RomInfo()
                    {
                        Mobile = string.Empty,
                        Password = string.Empty,
                        IsPush = pushStatus
                    };
                    IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
                }
            }
        }

        public TextBlock FindVisualChild(DependencyObject obj, string childItemName)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is TextBlock)
                {
                    if (childItemName.Equals((child as TextBlock).Name))
                    {
                        return child as TextBlock;
                    }
                }
                else
                {
                    TextBlock childOfChild = FindVisualChild(child, childItemName);
                    if (childOfChild != null)
                    {
                        if (childItemName.Equals((childOfChild as TextBlock).Name))
                        {
                            return childOfChild;
                        }
                    }
                }
            }
            return null;
        }
        #endregion 推送

        #region 退出
        private void imgUserExit_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                MessageToastManager.Show("您还未登录！");
                return;
            }
            if (MessageBoxResult.OK != MessageBox.Show("退出后部分功能将无法使用，确认退出？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel))
            {
                return;
            }
            var romInfoString = IsolatedStorageHelper.Instance.ReadText(Keys.ROM_FILENAME);
            if (!string.IsNullOrWhiteSpace(romInfoString))
            {
                RomInfo romInfo = JSONHelper.Deserialize<RomInfo>(romInfoString);
                if (romInfo != null)
                {
                    romInfo.IsPush = true;
                    romInfo.Mobile = string.Empty;
                    romInfo.Password = string.Empty;
                }
                this.tgsPush.IsChecked = true;
                IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
            }
            MessageToastManager.Show("操作成功，部分功能将无法使用！", MessageToastLevel.Warn);
        }
        #endregion 退出

        private void imageQuestion_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.gridSettingAbout.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingMain.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingSuggest.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingQA.Visibility = System.Windows.Visibility.Visible;
        }

        private void imageFeedback_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //TODO 反馈意见的列表接口呢，只存在本地？
            this.gridSettingAbout.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingMain.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingSuggest.Visibility = System.Windows.Visibility.Visible;
            this.gridSettingQA.Visibility = System.Windows.Visibility.Collapsed;
            if (OnSuggestListTab != null)
            {
                OnSuggestListTab(sender, e);
            }

        }

        private void imageVersionUpdate_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            new MarketplaceDetailTask().Show();
        }

        private void imageAbout_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.gridSettingAbout.Visibility = System.Windows.Visibility.Visible;
            this.gridSettingMain.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingSuggest.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingQA.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void imageCache_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }

        private void txtFeedbackContent_GotFocus(object sender, RoutedEventArgs e)
        {
            if (AppResources.UserCenterConfigInputFeedbackContent.Equals(this.txtFeedbackContent.Text.Trim()))
            {
                this.txtFeedbackContent.Text = string.Empty;
            }
        }

        private void txtFeedbackContent_LostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtFeedbackContent.Text.Trim()))
            {
                this.txtFeedbackContent.Text = AppResources.UserCenterConfigInputFeedbackContent;
            }
        }

        private void txtFeedbackContract_GotFocus(object sender, RoutedEventArgs e)
        {
            if (AppResources.UserCenterConfigInputFeedbackContract.Equals(this.txtFeedbackContract.Text.Trim()))
            {
                this.txtFeedbackContract.Text = string.Empty;
            }
        }

        private void txtFeedbackContract_LostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtFeedbackContract.Text.Trim()))
            {
                this.txtFeedbackContract.Text = AppResources.UserCenterConfigInputFeedbackContract;
            }
        }

        #region 意见反馈

        #region 反馈意见列表
        public event EventHandler OnSuggestListTab, OnWriteSuggestTap;



        private bool flagSuggestList, flagSuggestWrite;
        private Storyboard storySuggestList, storySuggestWrite;

        private void ImageWriteSuggest_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //this.gridSettingSuggestWrite.Visibility = Visibility.Visible;
            //this.gridSettingSuggestList.Visibility = Visibility.Collapsed;
            if (OnWriteSuggestTap != null)
            {
                this.OnWriteSuggestTap(sender, e);
            }
        }

        private void Grid_WriteSuggest_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (!this.flagSuggestList)
            {
                //this.OpenMenuStorySuggestList();
                this.flagSuggestList = true;
            }
            else
            {
                if (this.storySuggestList != null)
                {
                    this.storySuggestList.Stop();
                    this.storySuggestList.Children.Clear();
                    this.storySuggestList = null;
                }
                this.flagSuggestList = false;
            }
        }



        void story_Completed(object sender, EventArgs e)
        {

        }
        #endregion 反馈意见列表

        private void ImageSettingSuggestWrite_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }

        private void GridSettingSuggestWrite_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (!this.flagSuggestWrite)
            {
                //this.OpenMenuStoryBySuggestWrite();
                this.flagSuggestWrite = true;
            }
            else
            {
                if (this.storySuggestWrite != null)
                {
                    this.storySuggestWrite.Stop();
                    this.storySuggestWrite.Children.Clear();
                    this.storySuggestWrite = null;
                }
                this.flagSuggestWrite = false;
            }
        }

        

        //private void OpenMenuStorySuggestList()
        //{
        //    try
        //    {
        //        storySuggestList = new Storyboard();
        //        DoubleAnimation animation = new DoubleAnimation();
        //        this.gridSettingSuggestListNavigate.Projection = new PlaneProjection();
        //        storySuggestList.Completed += story_Completed;
        //        animation.Duration = (Duration)TimeSpan.FromSeconds(0.5);
        //        animation.To = -20.0;
        //        animation.From = 0.0;
        //        Storyboard.SetTarget(animation, gridSettingSuggestListNavigate.Projection);
        //        Storyboard.SetTargetProperty(animation, new PropertyPath("(PlaneProjection.LocalOffsetY)", new object[0]));
        //        storySuggestList.Children.Add(animation);
        //        if (this.LayoutRoot.Resources.Contains("story_gridSettingSuggestListNavigate"))
        //        {
        //            this.LayoutRoot.Resources.Remove("story_gridSettingSuggestListNavigate");
        //            this.LayoutRoot.Resources.Add("story_gridSettingSuggestListNavigate", storySuggestList);
        //            storySuggestList.Begin();
        //        }
        //        else
        //        {
        //            this.LayoutRoot.Resources.Add("story_gridSettingSuggestListNavigate", storySuggestList);
        //            storySuggestList.Begin();
        //        }
        //    }
        //    catch { }
        //}

        //private void OpenMenuStoryBySuggestWrite()
        //{
        //    try
        //    {
        //        storySuggestWrite = new Storyboard();
        //        DoubleAnimation animation = new DoubleAnimation();
        //        this.gridSettingSuggestWriteNavigate.Projection = new PlaneProjection();
        //        storySuggestWrite.Completed += story_Completed;
        //        animation.Duration = (Duration)TimeSpan.FromSeconds(0.5);
        //        animation.To = -20.0;
        //        animation.From = 0.0;
        //        Storyboard.SetTarget(animation, gridSettingSuggestWriteNavigate.Projection);
        //        Storyboard.SetTargetProperty(animation, new PropertyPath("(PlaneProjection.LocalOffsetY)", new object[0]));
        //        storySuggestWrite.Children.Add(animation);
        //        if (this.LayoutRoot.Resources.Contains("story_gridSettingSuggestWriteNavigate"))
        //        {
        //            this.LayoutRoot.Resources.Remove("story_gridSettingSuggestWriteNavigate");
        //            this.LayoutRoot.Resources.Add("story_gridSettingSuggestWriteNavigate", storySuggestWrite);
        //            storySuggestWrite.Begin();
        //        }
        //        else
        //        {
        //            this.LayoutRoot.Resources.Add("story_gridSettingSuggestWriteNavigate", storySuggestWrite);
        //            storySuggestWrite.Begin();
        //        }
        //    }
        //    catch { }
        //}
        #endregion 意见反馈








    }
}

